<?php
namespace app\appclass\controller;
use app\AdminRest;

use app\ApiRest;
use app\appclass\model\CardList;
use app\appclass\model\CardLog;
use app\appclass\model\CardTime;
use app\appclass\model\CardUser;
use app\appclass\model\CardWater;
use app\appclass\model\ClassCard;
use app\appclass\model\ClassCate;
use app\appclass\model\ClassCoach;
use app\appclass\model\ClassDate;
use app\appclass\model\ClassLable;
use app\appclass\model\ClassList;
use app\appclass\model\ClassRecord;
use app\appclass\model\ClassRoom;
use app\appclass\model\Lable;
use app\appclass\model\StoreInfo;
use app\card\model\Company;
use app\shop\model\IndexUser;
use app\shop\model\IndexUserInfo;
use think\App;
use think\facade\Db;


class IndexStaffCard extends ApiRest
{

    protected $model;

    protected $record_model;

    protected $lable_model;

    protected $room_model;

    protected $date_model;

    protected $store_id;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model       = new CardUser();

        $this->record_model= new ClassRecord();

        $this->card_model  = new ClassList();

        $this->log_model   = new CardLog();

        $this->date_model  = new ClassDate();

        $this->store_id = $this->model->getStoreId($this->getUserId());

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-02 17:41
     * @功能说明:用户会员列表
     */
    public function cardUserList(){

        $input = $this->_param;

        $dis[] = ['a.adviser','=',$this->getUserId()];

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //发卡时间
        if(!empty($input['create_start_time'])&&!empty($input['create_end_time'])){

            $create_start_time = $input['create_start_time'];

            $create_end_time   = $input['create_end_time'];

            $dis[] = ['a.create_time','between',"$create_start_time,$create_end_time"];

        }
        //有效期
        if(!empty($input['end_start_time'])&&!empty($input['end_end_time'])){

            $end_start_time = $input['end_start_time'];

            $end_end_time   = $input['end_end_time'];

            $dis[] = ['a.end_time','between',"$end_start_time,$end_end_time"];

        }
        //卡类型
        if(isset($input['card_type'])){

            $dis[] = ['a.card_type','=',$input['card_type']];

        }
        //体验卡/会员卡
        if(isset($input['effect'])){

            $dis[] = ['a.effect','=',$input['effect']];

        }
        //状态
        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.nickName','like','%'.$input['name'].'%'];

            $where[] = ['a.mobile','like','%'.$input['name'].'%'];
        }

        $data = $this->model->dataList($dis,10);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-03 10:00
     * @功能说明:会员卡详情
     */
    public function cardUserInfo(){

        $input = $this->_param;

        $dis = [


            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-20 10:14
     * @功能说明:会员预约列表
     */
    public function cardAppRecord(){

        $input = $this->_param;

        $dis = [

            'user_id' => $input['user_id'],

            'card_id' => $input['card_id']
        ];

        $record_model = new ClassRecord();

        $data = $record_model->dataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-03 14:18
     * @功能说明:停卡
     */
    public function stopCard(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        if($data['status']==4){

            $this->errorMsg('已停卡');
        }

        if($data['status']==5){

            $this->errorMsg('已退卡,无法停卡');

        }

        $res = $this->model->controlCard($input['id'],4,$this->_uniacid,$this->getUserId());

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-03 14:18
     * @功能说明:退卡
     */
    public function returnCard(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        if($data['status']==5){

            $this->errorMsg('已退卡,无法停卡');

        }

        $res = $this->model->controlCard($input['id'],5,$this->_uniacid,$this->getUserId());

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-03 15:02
     * @功能说明:删除卡
     */
    public function delCard(){

        $input = $this->_input;

        $res = $this->model->controlCard($input['id'],-1,$this->_uniacid,$this->getUserId());

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-12 11:19
     * @功能说明:绑定会员
     */
    public function cardUserAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;


        $res = $this->model->bindUser($input);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:01
     * @功能说明:添加会员卡
     */

    public function cardAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->card_model->adminDataAdd($input);

        return $this->success($res);


    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:18
     * @功能说明:会员卡详情
     */
    public function cardInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->card_model->dataInfo($dis);

        $card_time_model = new CardTime();

        $week['week'] = $card_time_model->where(['card_id'=>$data['id']])->group('week')->column('week');

        $week['time'] = $card_time_model->where(['card_id'=>$data['id']])->group('start_time')->select()->toArray();

        $week['week'] = !empty($week['week'])?array_values($week['week']):[];

        $data['card_time'] = $week;

        if(!empty($data['class_card'])){

            $class_model = new ClassList();

            foreach ($data ['class_card'] as &$v){

                $v['class_name'] = $class_model->where(['id'=>$v['class_id']])->value('title');

                $v['cate_id']    = $class_model->where(['id'=>$v['class_id']])->value('cate_id');

            }
        }

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:19
     * @功能说明:编辑会员卡
     */
    public function cardUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        //$this->model->check($input);

        $res = $this->card_model->adminDataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-30 17:33
     * @功能说明:续卡
     */
    public function renewCard(){

        $input = $this->_input;

        $log_model = new CardLog();

        $water_model= new CardWater();

        $card = $this->model->dataInfo(['id'=>$input['id']]);

        Db::startTrans();

        $insert = [

            'uniacid'      => $this->_uniacid,

            'card_user_id' => $input['id'],

            'status'       => 7,

            'create_time'  => time(),

            'price'        => $input['price'],

            'value'        => !empty($input['value'])?$input['value']:0,

            'before_value' => $card['true_value'],

            'after_value'  => !empty($input['value'])?$input['value']+$card['true_value']:$card['true_value'],

            'before_end_time' => $card['end_time'],

            'after_end_time'  => $input['end_time'],

            'text'          => $input['text'],

        ];

        $res = $log_model->insert($insert);

        if($res==0){

            Db::rollback();

            $this->errorMsg('续开失败');

        }

        $update = [

            'pay_price' => round($card['pay_price']+$input['price'],2),

            'true_value'=> $insert['after_value'],

            'end_time'  => $insert['after_end_time']

        ];

        $res = $this->model->dataUpdate(['id'=>$input['id']],$update);

        if($res==0){

            Db::rollback();

            $this->errorMsg('续开失败');

        }
        //期限卡没有
        if($card['card_type']!=0){
            //变更余额明细
            $res = $water_model->renewCardWater($card,$input['value']);

            if($res==0){

                Db::rollback();

                $this->errorMsg('续开失败');

            }

        }

        Db::commit();

        return $this->success($res);

    }







}
